package cn.cque.service.impl;

import cn.cque.dao.ProductDao;
import cn.cque.entity.Category;
import cn.cque.entity.PageResult;
import cn.cque.entity.Product;
import cn.cque.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: XWC
 * @ClassName: ProductServiceImpl
 * @Date: 2020/10/4
 * @Time: 18:37
 * @Description:
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductDao productDao;

    @Override
    public List<Product> findHot() {
        PageHelper.startPage(1, 8);
        return productDao.findHot(1);
    }

    @Override
    public List<Product> findNew() {
        PageHelper.startPage(1, 8);
        return productDao.findNew();
    }

    @Override
    public Product findById(String id) {
        return productDao.findById(id);
    }

    @Override
    public PageResult<Product> findByCid(String cid, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Product> productList = productDao.findByCid(cid);
        PageInfo<Product> pageInfo=new PageInfo<>(productList);
        PageResult<Product> pageResult=new PageResult<>();
        pageResult.setList(productList);
        pageResult.setTotalPage(pageInfo.getPages());
        pageResult.setCurrentPage(pageInfo.getPageNum());
        int total =(int)pageInfo.getTotal();
        pageResult.setTotalNum(total);
        return pageResult;
    }

    @Override
    public List<Product> findAll() {
        List<Product> productList = productDao.findAll();
        return productList;
    }

    @Override
    @Transactional
    public Integer addProduct(Product product) {
        product.setCreateDate(new Date());
        product.setIsHot(0);
        product.setFlag(0);
        return productDao.addProduct(product);
    }

    @Override
    @Transactional
    public Integer deleteProduct(String id) {
        return productDao.deleteProduct(id);
    }

    @Override
    @Transactional
    public Integer updateProduct(Product product) {
        return productDao.updateProduct(product);
    }
}